﻿using System.Web;
using Microsoft.Practices.Unity;

namespace WAMM.WebUI.Infrastructure {
    public static class MvcUnity {
        private const string WammUnityContainer = "WAMM.UnityContainer";

        public static IUnityContainer Container {
            get {
                if (!ContainerExists) {
                    UnityContainerInHttpContext = new UnityContainer();
                }
                return UnityContainerInHttpContext;
            }
        }

        public static bool ContainerExists {
            get {
                return UnityContainerInHttpContext != null;
            }
        }

        private static IUnityContainer UnityContainerInHttpContext {
            get { 
                return HttpContext.Current.Items[WammUnityContainer] as IUnityContainer;
            }
            set {
                HttpContext.Current.Items[WammUnityContainer] = value;
            }
        }
        public static void RemoveContainer() {
            UnityContainerInHttpContext = null;
        }
    }
}